package edu.hawaii.duedates.webapp;

import static org.junit.Assert.assertNotNull;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
import edu.hawaii.duedates.datastore.DueDatesConfig;
import edu.hawaii.duedates.datastore.User;
import edu.hawaii.duedates.libraries.UhLibrary;

/**
 * Test HomePage.
 * @author Daniel Arakaki, Jeho Jung, Arthur Shum, Daniel Tian.
 *
 */
public class TestHomePage {
  
  private static final String resultstable = "resultstable";
  
  private DueDatesConfig config;

  /**
   * Set up process before the Test Case. 
   */
  @Before
  public void setUp() {
     this.config = new DueDatesConfig();
     User user = new User("myname", "myemail@example.com","mypassword");
     
     String login1 = System.getProperty("DUEDATES_TESTUSER1_ID");
     String password1 = System.getenv("DUEDATES_TESTUSER1_LASTNAME");
     UhLibrary uh1 = new UhLibrary(login1, password1);
     user.addLibrary(uh1);

     String login2 = System.getenv("DUEDATES_TESTUSER2_ID");
     String password2 = System.getenv("DUEDATES_TESTUSER2_LASTNAME");     
     UhLibrary uh2 = new UhLibrary(login2, password2);     
     user.addLibrary(uh2);
     
     UhLibrary uh3 = new UhLibrary("fakelogin", "fakepassword");
     user.addLibrary(uh3);
     
     this.config.addUser(user);
     this.config.setSmtpServer("mysmtpserver");
  }
  
  /**
   * Test HomePage.
   */
  @Test
  public void testHomePage() {
    // use the alternate DueDatesApplication constructor
    WicketTester tester = new WicketTester(new DueDatesApplication(this.config));
    tester.startPage(LoginPage.class);
    tester.assertRenderedPage(LoginPage.class);
    // login with a test user account
    FormTester formTester = tester.newFormTester("form");
    formTester.setValue("email", "myemail@example.com");
    formTester.setValue("password", "mypassword");
    formTester.submit();
    
    tester.assertRenderedPage(HomePage.class);
    assertNotNull("welcome rendered", tester.getTagByWicketId("welcome"));
    assertNotNull("alerts rendered", tester.getTagByWicketId("alerts"));
    assertNotNull("logout rendered", tester.getTagByWicketId("logout"));
    assertNotNull("libraryrow rendered", tester.getTagByWicketId("libraryrow"));
    assertNotNull("libraryname rendered", tester.getTagByWicketId("libraryname"));
    assertNotNull("libraryform rendered", tester.getTagByWicketId("libraryform"));
    assertNotNull("filter rendered", tester.getTagByWicketId("filter"));
    assertNotNull("alllibrariesform rendered", tester.getTagByWicketId("alllibrariesform"));
    assertNotNull("feedback rendered", tester.getTagByWicketId("feedback"));
    
    tester.assertInvisible(resultstable);
    tester.assertInvisible("resultstable:libraryhead");
    tester.assertInvisible("resultstable:titlehead");
    tester.assertInvisible("resultstable:duedatehead");
    tester.assertInvisible("resultstable:resultsrow");
    
    FormTester formtester1 = tester.newFormTester("libraryrow:0:libraryform");
    formtester1.submit();
    tester.assertInvisible(resultstable);
    
    //tester.debugComponentTrees();

    FormTester formtester2 = tester.newFormTester("libraryrow:1:libraryform");
    formtester2.submit();
    tester.assertVisible(resultstable);
        
    FormTester formtester3 = tester.newFormTester("libraryrow:2:libraryform");
    formtester3.submit();
    tester.assertInvisible(resultstable);
    
    FormTester formtester4 = tester.newFormTester("alllibrariesform");
    formtester4.submit();
    tester.assertVisible(resultstable);
    
    tester.clickLink("resultstable:libraryhead");
    tester.clickLink("resultstable:titlehead");
    tester.clickLink("resultstable:duedatehead");
    
    //tester.dumpPage();

    
    tester.clickLink("logout");
    tester.assertRenderedPage(LoginPage.class);
  }
}


